1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.util.concurrent;
18  
19  import com.google.common.collect.ImmutableList;
20  
21  import junit.framework.TestCase;
22  
23  import java.util.List;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.TimeUnit;
26  
27  /**
28   * Tests for {@link AbstractListeningExecutorService}.
29   *
30   * @author Colin Decker
31   */
32  public class AbstractListeningExecutorServiceTest extends TestCase {
33  
34    public void testSubmit() throws Exception {
35      /*
36       * Mostly just tests that ListenableFutureTasks are created and run; tests for
37       * ListenableFutureTask should ensure that listeners are called correctly.
38       */
39  
40      TestListeningExecutorService e = new TestListeningExecutorService();
41  
42      TestRunnable runnable = new TestRunnable();
43      ListenableFuture<?> runnableFuture = e.submit(runnable);
44      assertTrue(runnableFuture instanceof ListenableFutureTask);
45      assertTrue(runnableFuture.isDone());
46      assertTrue(runnable.run);
47  
48      ListenableFuture<String> callableFuture = e.submit(new TestCallable());
49      assertTrue(callableFuture instanceof ListenableFutureTask);
50      assertTrue(callableFuture.isDone());
51      assertEquals("foo", callableFuture.get());
52  
53      TestRunnable runnable2 = new TestRunnable();
54      ListenableFuture<Integer> runnableFuture2 = e.submit(runnable2, 3);
55      assertTrue(runnableFuture2 instanceof ListenableFutureTask);
56      assertTrue(runnableFuture2.isDone());
57      assertTrue(runnable2.run);
58      assertEquals((Integer) 3, runnableFuture2.get());
59    }
60  
61    private static class TestRunnable implements Runnable {
62      boolean run = false;
63  
64      @Override
65      public void run() {
66        run = true;
67      }
68    }
69  
70    private static class TestCallable implements Callable<String> {
71      @Override
72      public String call() {
73        return "foo";
74      }
75    }
76  
77    /**
78     * Simple same thread listening executor service that doesn't handle shutdown.
79     */
80    private static class TestListeningExecutorService extends AbstractListeningExecutorService {
81  
82      @Override
83      public void execute(Runnable runnable) {
84        assertTrue(runnable instanceof ListenableFutureTask);
85        runnable.run();
86      }
87  
88      @Override
89      public void shutdown() {
90      }
91  
92      @Override
93      public List<Runnable> shutdownNow() {
94        return ImmutableList.of();
95      }
96  
97      @Override
98      public boolean isShutdown() {
99        return false;
100     }
101 
102     @Override
103     public boolean isTerminated() {
104       return false;
105     }
106 
107     @Override
108     public boolean awaitTermination(long timeout, TimeUnit unit) {
109       return false;
110     }
111   }
112 }